//
// Created by hsj on 2022/3/10.
//

#include "t_vector_valarray_array.h"
namespace t_vector_valarray_array{
    void show(double &val){
        cout<<val<<endl;
    }
    //测试
    void test(){
        //C++11初始化列表--禁止窄化
        double init_array[10]{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0};
        vector<double>
        ved1(10),
        //C++11初始化列表--禁止窄化
        ved2{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0},
        ved3(10);
        //初始化
        copy(init_array,init_array + 10,ved1.begin());
        valarray<double>
        val1(10),
        val2(10),
        val3(10);
        //初始化
        for (int i = 0; i < 10; ++i) {
            val1[i] = i + 1.0;
            val2[i] = i + 1.0;
        }

        array<double,10>
        //C++11初始化列表--禁止窄化
        arr1{1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0},
        arr2,
        arr3;
        //初始化
        for (int i = 0; i < 10; ++i) {
            arr2[i] = i + 1.0;
        }

        //相加赋值给第三个
        transform(ved1.begin(),ved1.end(),ved2.begin(),ved3.begin(),plus<double>());
        for_each(ved3.begin(),ved3.end(),show);
        cout<<"---------------------------------------------------------------------------"<<endl;
//        val3 = val1 + val2;
//        for (double &v:val3){
//            cout<<v<<endl;
//        }
        transform(begin(val1),end(val1),begin(val2),begin(val3),plus<double>());
        for_each(begin(val3),end(val3),show);
        cout<<"---------------------------------------------------------------------------"<<endl;
        transform(arr1.begin(),arr1.end(),arr2.begin(),arr3.begin(),plus<double>());
        for_each(arr3.begin(),arr3.end(),show);


        //每个元素扩大2.5倍
        cout<<"*******************************************************************************"<<endl;
        transform(ved3.begin(),ved3.end(),ved3.begin(),bind1st(multiplies<double>(),2.5));
        for_each(ved3.begin(),ved3.end(),show);
        cout<<"*******************************************************************************"<<endl;
//        val3 *= 2.5;
//        for (double &v:val3){
//            cout<<v<<endl;
//        }
        transform(begin(val3),end(val3),begin(val3),bind1st(multiplies<double>(),2.5));
        for_each(begin(val3),end(val3),show);
        cout<<"*******************************************************************************"<<endl;
        transform(arr3.begin(),arr3.end(),arr3.begin(),bind1st(multiplies<double>(),2.5));
        for_each(arr3.begin(),arr3.end(),show);
    }
}